import gpio
import os
import i2c
sht30_addr = 0x44 << 1
class SHT30:
    def __init__(self):
        gpio.gpio_init(0)
        func = gpio.query_func_value(0, 'I2C1_SDA')
        gpio.set_func(0, func)

        gpio.gpio_init(1)
        func = gpio.query_func_value(1, 'I2C1_SCL')
        gpio.set_func(1, func)



    def temperature(self):
        r, d = i2c.write_read(1, sht30_addr, [0xE0, 0x00], 6)
        if r == 0:
            t = (d[0] << 8) | d[1]
            t = t & ~0x3
            t = t * 175 // 65536 - 45
            return t
        else:
            return None

    def humidity(self):
        r, d = i2c.write_read(1, sht30_addr, [0xE0, 0x00], 6)
        if r == 0:
            h = (d[3] << 8) | d[4]
            h = h & ~0x3
            h = h * 100 // 65536
            return h
        else:
            return None

    def open(self):
        i2c.i2c_init(1, 400000)
        i2c.write(1, sht30_addr, [0x22, 0x36])

    def close(self):
        i2c.i2c_deinit(1)


